package com.hm.ibsm.web.mg;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hm.common.util.CommonUtil;
import com.hm.ibsm.auto.helper.SystemSessionHolder;
import com.hm.ibsm.service.MenuService;
import com.hm.ibsm.service.RoleService;
import com.hm.ibsm.util.BaseController;
import com.hm.ibsm.vo.auth.UserMenuVo;
import com.hm.ibsm.vo.role.RoleVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月12日 下午10:44:41
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
@RequestMapping("/mg/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;

	private String MODEL_NAME = UserController.class.getName();

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public String roleMg() {
		return "mg/role";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public String addRole() {
		return "mg/role/addRole";
	}

	@RequestMapping(value = "/detail.do", method = RequestMethod.GET)
	public ModelAndView detailRole(RoleVo role) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", roleService.getRoleById(role.getId()));
			return new ModelAndView("mg/role/detailRole", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)
	public ModelAndView updateRole(RoleVo role) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("result", roleService.getRoleById(role.getId()));
			return new ModelAndView("mg/role/updateRole", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/addRole.do", method = RequestMethod.POST)
	public String saveAddRole(RoleVo role) {
		try {
			roleService.addRole(role);
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/deleteRole.do", method = RequestMethod.GET)
	public String saveDeleteRole(RoleVo role) {
		try {
			roleService.deleteRole(role.getId());
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/updateRole.do", method = RequestMethod.POST)
	public String saveUpdateRole(RoleVo role) {
		try {
			roleService.updateRole(role.getId(), role);
			return roleMg();
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public @ResponseBody String query(RoleVo role) {
		return json(roleService.query(SystemSessionHolder.pageParam(), CommonUtil.of(role.getRoleName()))).toString();
	}
	
	@RequestMapping(value = "/settingMenus.do", method = RequestMethod.GET)
	public ModelAndView settingMenus(@RequestParam(name = "role", required = true) String role) {
		try {
			HashMap<String, Object> result = new HashMap<String, Object>();
			{
				result.put("menuTree", loopMenus(menuService.loadMenus(), new JSONArray(), "0", true).toJSONString());
			}
			return new ModelAndView("mg/role/settingMenus", result);
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
			return sessionTimeOut();
		}
	}
	
	private JSONArray loopMenus(List<UserMenuVo> menus, JSONArray items, String parentId, boolean spread) {
		for (UserMenuVo menu : menus) {
			JSONObject item = new JSONObject();
			{
				item.put("checkboxValue", menu.getMenuId());
				item.put("name", menu.getMenuName());
				item.put("checked", true);
				item.put("name", menu.getMenuName());
				if (spread) {
					item.put("spread", spread);
					spread = false;
				}
			}
			if (menu.isHasChild()) {
				item.put("children", loopMenus(menu.getChilds(), new JSONArray(), menu.getMenuId(), false));
			}
			items.add(item);
		}
		return items;
	}
}
